const express = require('express')
const app = express()
const cors = require('cors')//cors 跨域模块
const adminRouter = require('./route/admin')
const pool = require('./pool')
const jwt = require('jsonwebtoken')
const key = 'my key'
const captcha = require('svg-captcha')
app.use(cors({
    origin:['http://localhost:8080','http://127.0.0.1:5500','http://localhost:8081']//允许这两个地址跨域
}))
app.use(express.urlencoded({
    extended:false
}))
//核酸预约 获取
app.get('/hsyylist',(req,res,next)=>{
    pool.query('select * from hx_hsyy;',(err,result)=>{
        if(err){
            next(err)
            return
        }
        res.send({code:200,msg:'成功',data:result})
    })
})

//核酸预约 提交
app.post('/hsyy',(req,res,next)=>{
    let date = new Date()
    date = date/1000
    let name = req.body.name
    let id_card = req.body.id_card
    let phone = req.body.phone
    pool.query(`insert into hx_hsyy values(null,?,?,?,${date},"未完成")`,[name,id_card,phone],(err,result)=>{
        if(err){
            next(err)
            return
        }
        if(result.affectedRows==0){
            res.send({code:201,msg:'error'})
            return
        }
        res.send({code:200,msg:'ok'})
       

    })
})


//index warp
app.get('/indexwarp',(req,res,next)=>{
    pool.query('select * from index_warp_list1;',(err,result)=>{
        if(err){
            next(err)
            return
        }
        res.send({code:200,data:result})
    })
})
app.get('/indexwarp1',(req,res,next)=>{
    pool.query('select * from index_warp_list2;',(err,result)=>{
        if(err){
            next(err)
            return
        }
        res.send({code:200,data:result})
    })
})
app.get('/indexwarp2',(req,res,next)=>{
    pool.query('select * from index_warp_list3;',(err,result)=>{
        if(err){
            next(err)
            return
        }
        res.send({code:200,data:result})
    })
})
app.get('/indexwarp3',(req,res,next)=>{
    pool.query('select * from index_warp_list4;',(err,result)=>{
        if(err){
            next(err)
            return
        }
        res.send({code:200,data:result})
    })
})

// 获取 token接口 使用jwt签名
app.get('/gettoken',(req,res,next)=>{
    let payload = {
        exp:Date.now()/1000 + 3600*24
    }
    let token = jwt.sign(payload,key)
    res.send(token)
})
// 获取验证码 发送 svg图 保存token 与 验证码 到数据库
app.get('/getcode',(req,res,next)=>{
    let token = req.headers.authorization
    jwt.verify(token,key,(err,decoded)=>{
       if(err!=null)
       {
           res.send({msg:'token不合法'})
           return
       }
       let c = captcha.create({size:4,height:40,width:150,noise:4,fontSize:32})
       //长度4 高宽 40 * 150 干扰线 4条
       let data = c.data
       let text = c.text.toLowerCase()
       pool.query('insert into hx_code values(?,?);',[token,text],(err,result)=>{
           if(err)
           {
               next(err)
               return
           }
       })
        res.send({code:200,data:data})
    })
})

app.get('/login',(req,res,next)=>{
    let token = req.headers.authorization
    let uname = req.query.uname
    let upwd = req.query.upwd
    let ucode = req.query.ucode
    console.log('token',token);
    console.log('uname',uname)
    console.log('upwd',upwd);
    console.log('ucode',ucode)
    jwt.verify(token,key,(err,decoded)=>{
       if(err!=null)
       {
           res.send({msg:'token不合法'})
           return
       }
       pool.query('select * from hx_admin where uname=? and upwd=md5(?)',[uname,upwd],(err,result)=>{
           if(err)
           {
               next(err)
               return
           }
           if(result.length==0)
           {
               res.send({code:'201',msg:'用户名密码错误'})
               console.log(result);
               return
           }
           pool.query('select * from hx_code where hkey=? and hvalue=?',[token,ucode],(err,result)=>{
            if(err)
            {
                next(err)
                return
            }
            if(result.length==0)
            {
                res.send({code:'202',msg:'验证码错误'})
                return
            }
              res.send({code:200,msg:'登录成功'})
        })
       })
      
      
    })
   
})




app.listen(3000,()=>{
    console.log('Do not shut down the server when it is started...');
})



app.use((err,req,res,next)=>{
    console.log(err);
    res.status(500).send({code:500,msg:'服务器错误!'})
})